clc;clear;

%----- Impedances

z1g = 0.2i;
z2g = 0.4j;

z12 = 0.8i;
z13 = 0.4i;
z23 = 0.4i;

zf = 0.16i;

%----- Voltages

V1 = 1;
V2 = 1;
V3 = 1;

%----- Thevinin impedances

z11 = ((z12*(z13+z23)/(z12+z13+z23))+z2g)*z1g/(z1g+(z12*(z13+z23)/((z12+z13+z23))+z2g));
z22 = ((z12*(z13+z23)/(z12+z13+z23))+z1g)*z2g/(z2g+(z12*(z13+z23)/((z12+z13+z23))+z1g));
z33 = ((z13*z23)/(z13+z23+z12))+(((((z12*z13)/(z13+z23+z12))+z1g)*(((z12*z23)/(z13+z23+z12))+z2g))/((((z12*z13)/(z13+z23+z12))+z1g)+(((z12*z23)/(z13+z23+z12))+z2g)));

%----- Fault Currents

I1 = V1/(zf + z11);
I2 = V2/(zf + z22);
I3 = V3/(zf + z33);

%----- currents of G1 and G2 when fault occurs at bus 1

IG1_1 = I1*((z12*(z13+z23)/(z12+z13+z23))+z2g)/(((z12*(z13+z23)/(z12+z13+z23))+z2g)+z1g); 
IG2_1 = I1 - IG1_1;

%----- currents of G1 and G2 when fault occurs at bus 2

IG2_2 = I2*((z12*(z13+z23)/(z12+z13+z23))+z1g)/(((z12*(z13+z23)/(z12+z13+z23))+z1g)+z2g); 
IG1_2 = I2 - IG2_2;

%----- currents of G1 and G2 when fault occurs at bus 3

IG1_3 = I3*(((z12*z23)/(z13+z23+z12))+z2g)/((((z12*z23)/(z13+z23+z12))+z2g)+(((z12*z13)/(z13+z23+z12))+z1g)); 
IG2_3 = I3 - IG1_3;

%----- voltage change (∆V) when fault occurs at bus 1

delta_v1_1 = 0 - IG1_1*z1g;
delta_v2_1 = 0 - IG2_1*z2g;
delta_v3_1 = (-IG2_1)*(z12)/(z23+z13+z12)*z23 + (-IG2_1*z2g);

%----- voltage change (∆V) when fault occurs at bus 2

delta_v1_2 = 0 - IG1_2*z1g;
delta_v2_2 = 0 - IG2_2*z2g;
delta_v3_2 = (-IG1_2)*(z12)/(z23+z13+z12)*z13 + (-IG1_2*z1g);

%----- voltage change (∆V) when fault occurs at bus 3

delta_v1_3 = 0 - IG1_3*z1g;
delta_v2_3 = 0 - IG2_3*z2g;
delta_v3_3 = -V3+zf*I3;

%----- Bus voltages after the fault occurs at bus 1

Vf1_1 = V1 + delta_v1_1;
Vf2_1 = V2 + delta_v2_1;
Vf3_1 = V3 + delta_v3_1;

%----- Bus voltages after the fault occurs at bus 2

Vf1_2 = V1 + delta_v1_2;
Vf2_2 = V2 + delta_v2_2;
Vf3_2 = V3 + delta_v3_2;

%----- Bus voltages after the fault occurs at bus 3

Vf1_3 = V1 + delta_v1_3;
Vf2_3 = V2 + delta_v2_3;
Vf3_3 = V3 + delta_v3_3;

%----- The short circuit line currents when fault occurs at bus 1

If21_1 = (Vf2_1 - Vf1_1)/z12;
If31_1 = (Vf3_1 - Vf1_1)/z13;
If23_1 = (Vf2_1 - Vf3_1)/z23;

%----- The short circuit line currents when fault occurs at bus 2

If12_2 = (Vf1_2 - Vf2_2)/z12;
If13_2 = (Vf1_2 - Vf3_2)/z13;
If32_2 = (Vf3_2 - Vf2_2)/z23;

%----- The short circuit line currents when fault occurs at bus 3

If12_3 = (Vf1_3 - Vf2_3)/z12;
If13_3 = (Vf1_3 - Vf3_3)/z13;
If23_3 = (Vf2_3 - Vf3_3)/z23;